/*
 * FileLocator.java
 *
 * Copyright (C) 2005-2008 Tommi Laukkanen
 * http://www.substanceofcode.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package com.substanceofcode.data.midp;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

//import com.substanceofcode.localization.LocaleManager;

/**
 * <p>This class encompasses all the information needed to 
 * recover a 'File' blob from the underlying RMS. </p>
 * 
 * i.e. Which RecordStores and which records the file blob is in.<br>
 * It also has some additional anciliary information, such as file name and mime type.
 * 
 * It also contains methods to recover and store a FileLocator to a DataInputStream and DataOutputStream respectively
 * @author Barry
 *
 */
class FileLocator
{
    /**
     * The size of the file this locator points to. (in bytes)
     */
    public final int    iSize;

    /**
     * Since we use Integer.toString() of numbers anyway as the 'names', we just store the integers.
     */
    public final int[]  iRecordStoreNames;

    public final int[]  iRecordIds;

    public final String iFileName;
    public final String iMimeType;

    public FileLocator( String aFileName, String aMimeType, int aSize, int[] aRecordStoreNames, int[] aRecordIds )
    {
        /*
         * Make sure that neither stores, nor record ids are null, and that they are the same length.
         */

        if( aRecordStoreNames == null || aRecordIds == null )
        {
            //throw new IllegalArgumentException(LocaleManager.getMessage("file_locator_store_null"));
            throw new IllegalArgumentException( "file_locator_store_null" );
        }
        if( aRecordStoreNames.length != aRecordIds.length )
        {
            //throw new IllegalArgumentException(LocaleManager.getMessage("file_locator_require_elements")
            //        + "(" + stores.length + ") _ (" + numbers.length + ")");
            throw new IllegalArgumentException( "file_locator_require_elements" + "(" + aRecordStoreNames.length + ") _ (" + aRecordIds.length + ")" );
        }

        this.iFileName = aFileName;
        this.iMimeType = aMimeType;
        this.iSize = aSize;

        this.iRecordStoreNames = new int[aRecordStoreNames.length];
        for( int i = 0; i < aRecordStoreNames.length; i++ )
        {
            iRecordStoreNames[i] = aRecordStoreNames[i];
        }
        this.iRecordIds = new int[aRecordIds.length];
        for( int i = 0; i < aRecordIds.length; i++ )
        {
            iRecordIds[i] = aRecordIds[i];
        }
    }

    public FileLocator( DataInputStream dis ) throws IOException
    {
        this.iFileName = dis.readUTF();
        this.iMimeType = dis.readUTF();
        this.iSize = dis.readInt();

        int arraySize = dis.readInt();
        this.iRecordStoreNames = new int[arraySize];
        for( int i = 0; i < arraySize; i++ )
        {
            iRecordStoreNames[i] = dis.readInt();
        }

        this.iRecordIds = new int[arraySize];
        for( int i = 0; i < arraySize; i++ )
        {
            iRecordIds[i] = dis.readInt();
        }
    }

    public void writeFileLocator( DataOutputStream dos ) throws IOException
    {
        dos.writeUTF( iFileName );
        dos.writeUTF( iMimeType );
        dos.writeInt( iSize );

        dos.writeInt( iRecordStoreNames.length );
        for( int i = 0; i < iRecordStoreNames.length; i++ )
        {
            dos.writeInt( iRecordStoreNames[i] );
        }
        for( int i = 0; i < iRecordIds.length; i++ )
        {
            dos.writeInt( iRecordIds[i] );
        }
    }
}